package com.nd.service;

import cn.hutool.core.date.DateTime;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.nd.dao.IssueMapper;
import com.nd.pojo.bean.Issue;
import com.nd.pojo.bean.IssueExample;
import com.nd.pojo.bo.IssueCreateBO;
import com.nd.pojo.vo.IssueCreateVO;
import com.nd.pojo.vo.IssueListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Description:
 * @author: Aaron
 * @create: 2021-06-06 11:43
 **/
@Service
public class IssueServiceImpl implements IssueService {
    @Autowired
    IssueMapper issueMapper;

    @Override
    public IssueListVO issueList(Integer page, Integer limit, String question, String sort, String order) {
        PageHelper.startPage(page,limit);
        IssueExample issueExample = new IssueExample();
        issueExample.setOrderByClause(sort + " " + order);
        IssueExample.Criteria criteria = issueExample.createCriteria();
        criteria.andDeletedEqualTo(false);
        if (question != null && !"".equals(question)) criteria.andQuestionLike("%" + question + "%");
        List<Issue> issues = issueMapper.selectByExample(issueExample);
        PageInfo<Issue> issuePageInfo = new PageInfo<>(issues);
        long total = issuePageInfo.getTotal();
        IssueListVO issueListVO = new IssueListVO();
        issueListVO.setTotal(total);
        issueListVO.setItems(issues);
        return issueListVO;
    }

    @Override
    public IssueCreateVO issueCreate(IssueCreateBO issueCreateBO) {
        Issue issue = new Issue();
        issue.setQuestion(issueCreateBO.getQuestion());
        issue.setAnswer(issueCreateBO.getAnswer());
        DateTime time = DateTime.now();
        issue.setAddTime(time);
        issue.setUpdateTime(time);
        issueMapper.issueCreate(issue);
        Integer id = issueMapper.issueGetId();
        IssueCreateVO issueCreateVO = IssueCreateVO.builder()
                                                   .id(id)
                                                   .question(issueCreateBO.getQuestion())
                                                   .answer(issueCreateBO.getAnswer())
                                                   .addTime(time)
                                                   .updateTime(time)
                                                   .build();


        return issueCreateVO;
    }

    @Override
    public Issue issueUpdate(Issue issue) {
        Issue issueBO = new Issue();
        issueBO.setId(issue.getId());
        issueBO.setQuestion(issue.getQuestion());
        issueBO.setAnswer(issue.getAnswer());
        issueBO.setAddTime(issue.getAddTime());
        DateTime time = DateTime.now();
        issueBO.setUpdateTime(time);
        issueBO.setDeleted(false);
        issueMapper.issueUpdate(issueBO);
        return issueBO;
    }

    @Override
    public void issueDelete(Issue issue) {
        issueMapper.issueDelete(issue.getId());
    }

}
